package club.banyuan.demo.file;

import java.io.*;
import java.net.Socket;

/**
 * @author chencong
 * @date 2020/12/11 上午9:25
 */
public class Client {
    public static void main(String[] args) throws Exception {
        System.out.println("客户端启动成功");
        //与服务器连接管道
        Socket socket = new Socket(Constants.SERVER_IP, Constants.PORT);
        //从管道中取出字节输出流,包装成缓冲字节输出流
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
        //得到一个缓冲字节输入流,连接本机文件和socket
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(Constants.SRC_FILE));
        //开始读取文件并传输
        byte[] buffer = new byte[1024];
        System.out.println("正在发送文件");
        int len;
        while ((len = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.flush();
        bis.close();
        //关闭管道中的输出流,(让服务器停止接收)
        socket.shutdownOutput();
        System.out.println("数据发送成功,等待服务器接收结果");
        //获取管道中的字节输入流,接收服务器的反馈信息,转化成字符输入流,转化成缓冲字符输入流
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        //服务器的反馈是一行话,直接读取一行并输出
        System.out.println(br.readLine());
    }
}
